<template>
    <div id="announcementList">
        <!-- 搜索框 -->
        <div id="searchBar">
            <el-input v-model="searchTheme" placeholder="请输入主题关键字" type="input" style="width: 180px;" clearable></el-input>
            <el-date-picker v-model="searchTime" type="date" placeholder="请选择公告时间"></el-date-picker>
            <el-button icon="el-icon-search" circle @click="search(true)" class="search"></el-button>
        </div>
        <!-- 表格 -->
        <el-table :data="announcementData" stripe border class="el-table">
            <el-table-column prop="id" label="id" align="center"></el-table-column>
            <el-table-column prop="theme" label="公告主题" align="center"></el-table-column>
            <el-table-column prop="content" label="公告内容" align="center"></el-table-column>
            <el-table-column prop="time" label="时间" align="center"></el-table-column>
            <el-table-column label="操作" fixed="right" align="center">
                <template slot-scope="scope">
                    <el-button @click="currentAnnouncement = scope.row; announcementEditShow = true;" type="primary"
                        icon="el-icon-edit"  size="mini"></el-button>
                    <el-button @click="currentAnnouncement = scope.row; announcementDeleteShow = true;" type="primary"
                        icon="el-icon-delete"  size="mini"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页按钮 -->
        <div id="paging">
            <el-pagination layout="prev, pager, next" :page-size="pageSize" :total="total"
                @current-change="pageCurrentChange">
            </el-pagination>
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="announcementDeleteShow" width="30%">
            <span>确定删除该公告么</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="announcementDeleteShow = false;">取 消</el-button>
                <el-button type="primary"
                    @click="deleteAnnouncement(currentAnnouncement.id); announcementDeleteShow = false;">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑提示框 -->
        <el-dialog title="编辑公告" :visible.sync="announcementEditShow">
            <el-form :model="currentAnnouncement" label-position="top">
                <el-form-item>
                    <span>id：{{ currentAnnouncement.id }}</span>
                </el-form-item>
                <el-form-item label="主题">
                    <el-input v-model="currentAnnouncement.theme" type="input" clearable></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="currentAnnouncement.content" type="textarea" autosize></el-input>
                </el-form-item>
                <el-form-item label="时间">
                    <el-date-picker type="date" placeholder="请选择日期" v-model="currentAnnouncement.time"></el-date-picker>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="announcementEditShow = false">取 消</el-button>
                <el-button type="primary" @click="updateAnnouncement(currentAnnouncement); announcementEditShow = false">提
                    交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            announcementData: [], // 所有的公告数据
            currentAnnouncement: {
                theme: '',
                content: '',
                time: '',
            }, // 当前选中的公告数据
            announcementDeleteShow: false, // 控制删除提示框的显示/隐藏
            announcementEditShow: false, // 控制编辑提示框的显示/隐藏
            currentPage: 1, // 当前页
            pageSize: 10, // 页大小
            total: 0, // 数据总数
            searchOrNot: false, // 是否搜索状态量，用于确定翻页后数据从那个方法得到
            searchTheme: '', // 搜索主题内容
            searchTime: '', // 搜索时间内容
        }
    },
    methods: {
        // 获取公告内容
        getAnnouncementData() {
            this.$axios({
                method: 'post',
                url: '/announcement/login/getAnnouncementPage/' + this.currentPage + '/' + this.pageSize,
            }).then(res => {
                res.data.data.data.forEach(element => { // 分割时间
                    element.time = element.time.slice(0, 10)
                });
                this.announcementData = res.data.data.data
                this.total = res.data.data.total
            })
        },
        // 删除公告
        deleteAnnouncement(id) {
            this.$axios({
                method: 'post',
                url: '/announcement/login/deleteAnnouncement/' + id
            }).then(res => {
                if (res.data) {
                    this.$router.go(0)
                    this.$message({
                        message: '删除成功',
                        type: 'success',
                        showClose: true,
                    })
                } else {
                    this.$message({
                        message: '删除失败！出现错误了X.X',
                        type: 'error',
                        showClose: true,
                    })
                }
            })
        },
        // 更新公告
        updateAnnouncement(item) {
            // 检查数据完整性
            if (this.currentAnnouncement.time == null || this.currentAnnouncement.time == '' || this.currentAnnouncement.theme == null || this.currentAnnouncement.theme == '' || this.currentAnnouncement.content == null || this.currentAnnouncement.content == '') {
                this.$message({
                    message: '请完善信息再提交',
                    type: 'error',
                    showClose: true
                })
                return
            }
            // 准备数据
            let data = new FormData()
            data.append('id', item.id)
            data.append('theme', item.theme)
            data.append('content', item.content)
            data.append('time', item.time)
            this.$axios({
                method: 'post',
                url: '/announcement/login/updateAnnouncementContent',
                data: data,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(res => {
                if (res.data) {
                    this.$router.go(0)
                    this.$message({
                        message: '删除成功',
                        type: 'success',
                        showClose: true,
                    })
                } else {
                    this.$message({
                        message: '删除失败！出现错误了X.X',
                        type: 'error',
                        showClose: true,
                    })
                }
            })
        },
        // 翻页事件
        pageCurrentChange(val) {
            this.currentPage = val // 更改当前页
            if (this.searchOrNot) { // 若搜索过，就用筛选搜索的方式查询数据
                this.search(false)
                return
            }
            this.getAnnouncementData()
        },
        // 筛选搜索
        search(buttonSearchOrNot) {
            if (buttonSearchOrNot) { // 若为按钮点击搜索,重设页状态
                this.currentPage = 1
                this.total = 0
            }
            this.searchOrNot = true // 改变搜索状态量
            let data = new FormData()
            if (this.searchTheme != null && this.searchTheme != '') data.append("theme", this.searchTheme)
            if (this.searchTime != null && this.searchTime != '') data.append("time", this.searchTime)
            this.$axios({
                method: 'post',
                url: '/announcement/login/searchByThemeAndTime/' + this.currentPage + '/' + this.pageSize,
                data: data,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then(res => {
                res.data.data.data.forEach(element => { // 分割时间
                    element.time = element.time.slice(0, 10)
                });
                this.announcementData = res.data.data.data
                this.total = res.data.data.total
            })
        }
    },
    mounted() {
        this.getAnnouncementData()
    }
}
</script>
<style lang="scss" scoped>
#searchBar {
    margin: 20px auto;
    display: flex;
    justify-content: center;
}
</style>
<style scoped="scope" scope>
/* 表格 */
.el-table {
    width: 80%;
    margin: 0 auto;
    border-radius: 20px;
}

.el-table__row {
    background-color: rgba(0, 0, 0, 0);
}

/deep/.el-table--border {
    border-color: #464646;
}

/deep/.el-table th.el-table__cell.is-leaf {
    border-color: #464646;
}

/deep/ .el-table thead tr>th {
    background-color: #202020 !important;
    color: #464646;
    border-color: #464646;
}

/deep/ .el-table tbody tr>td {
    background-color: #202020 !important;
    border-color: #464646;
}

/deep/.el-table::before,
/deep/.el-table__fixed-right::before {
    background-color: rgba(0, 0, 0, 0);
}
.cell .el-button--primary {
  margin-bottom: 10px;
}
/deep/.el-table__body-wrapper {
    border-bottom: 1px solid;
}
/* 分页 */

/deep/.el-pagination {
    display: flex;
    justify-content: center;
    margin: 20px auto;
    color: #202020;
}

/deep/.el-pagination button {
    color: #fff;
    background-color: #202020;
}

/deep/ .el-pager li.active {
    color: rgb(255, 150, 50);
}

/deep/ .number {
    color: #fff;
}

/deep/ .number:hover {
    color: rgb(255, 150, 50);
}

/deep/.el-pager li {
    background-color: #202020;
}

/* 搜索Bar 上传Bar */
.search {
    background-color: #202020;
    color: #fff;
}

/deep/#searchBar .el-input__inner,
/deep/#announcementUpload .el-input__inner,
/deep/#announcementUpload .el-textarea__inner {
    background-color: #202020;
    color: #fff;
    text-align: center;
}

/deep/#announcementUpload .el-input__inner {
    width: 300px;
}

/deep/.el-textarea__inner {
    min-height: 60px !important;
}

#searchBar .el-input,
#searchBar .el-select {
    margin: 0 20px;
}
/deep/.el-table__empty-block {
    background-color: #202020;
}
</style>